package com.situ.personal.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.personal.entity.PageInfo;
import com.situ.personal.entity.Role;
import com.situ.personal.entity.User;
import com.situ.personal.service.RoleService;
import com.situ.personal.service.UserRoleService;
import com.situ.personal.service.UserService;

@WebServlet("/userRole")
public class UserRoleServlet extends HttpServlet{
	private UserRoleService userRoleService = new UserRoleService();
	private UserService userService = new UserService();
	private RoleService roleService = new RoleService();

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		///req.setCharacterEncoding("UTF-8");
		
		String method = req.getParameter("method");
		if (method == null || "".equals(method)) {
			method = "findByPage";
		}
		
		switch (method) {
		case "findByPage":
			findByPage(req, resp);
			break;
		case "deleteById":
			deleteById(req, resp);
			break;
		case "insert":
			insert(req, resp);
			break;
		case "getUserRoleInsertPage":
			getUserRoleInsertPage(req, resp);
			break;
		default:
			break;
		}
	}

	private void getUserRoleInsertPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List<User> userList = userService.findAll();
		List<Role> roleList = roleService.findAll();
		req.setAttribute("userList", userList);
		req.setAttribute("roleList", roleList);
		System.out.println("userList: " + userList);
		System.out.println("roleList: " + roleList);
		
		req.getRequestDispatcher("/user_role_insert.jsp").forward(req, resp);
	}

	// DELETE FROM user_role WHERE user_id=9 AND role_id=14;
	private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String userId = req.getParameter("userId");
		String roleId = req.getParameter("roleId");
		boolean isSuccess =  userRoleService.deleteById(Integer.parseInt(userId), Integer.parseInt(roleId));
		
		resp.sendRedirect(req.getContextPath() + "/userRole?method=findByPage");
	}

	private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String pageNo = req.getParameter("pageNo");
		String pageSize = req.getParameter("pageSize");
		if (pageNo == null || "".equals(pageNo)) {
			pageNo = "1";
		}
		if (pageSize == null || "".equals(pageSize)) {
			pageSize = "5";
		}
		
		PageInfo pageInfo = userRoleService.findByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
		System.out.println(pageInfo);
		
		req.setAttribute("pageInfo", pageInfo);
		req.getRequestDispatcher("/user_role_page_list.jsp").forward(req, resp);
	}

	private void insert(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String userId = req.getParameter("userId");// userId:2
		// roleId传递多个值，会以数组的形式传递过来
		String[] roleIds = req.getParameterValues("roleId");// roleIds:[1, 2, 3]
		System.out.println("userId:" + userId);
		System.out.println("roleIds:" + Arrays.toString(roleIds));
		//把多个roleId'数据for循环转换成int类型
		ArrayList<Integer> roleIdList = new ArrayList<Integer>();
		for (String roleId : roleIds) {
			roleIdList.add(Integer.parseInt(roleId));
		}
		
		boolean isSuccess = userRoleService.insert(Integer.parseInt(userId), roleIdList);
		
		resp.sendRedirect(req.getContextPath() + "/userRole?method=findByPage");
		
		
		
	}
	
	
	
}